<template>
    <div class="createPost-container">
        <el-form ref="postForm" :model="postForm" class="form-container">
            <div class="createPost-main-container" style="margin-top: 20px;">

                <el-form-item label="任务名称" prop="filename" label-width="90px" class="postInfo-container-item">
                    {{postForm.filename}}
                </el-form-item>

                <el-form-item label-width="90px" prop="importance" label="重要性" class="postInfo-container-item">
                    <el-rate
                            v-model="postForm.importance"
                            :max="3"
                            :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
                            :low-threshold="1"
                            :high-threshold="3"
                            style="display:inline-block"
                            disabled
                    />
                </el-form-item>

                <el-form-item label-width="90px" label="指派" class="postInfo-container-item">
                    {{postForm.username}}
                </el-form-item>

                <el-form-item label-width="90px" label="文件" prop="attach_id" class="postInfo-container-item">
                    {{postForm.attach.origin_name}}
                </el-form-item>

                <el-form-item label-width="90px">
                    <el-button type="success" :disabled="loading" @click="submitForm">下载文件，开始任务</el-button>
                </el-form-item>

            </div>
        </el-form>
    </div>
</template>

<script>

    export default {
        name: 'assign',
        data() {
            return {
                postForm: null,
                loading: false,
                userListOptions: [],
            }
        },
        created() {
            const id = this.$route.params && this.$route.params.id
            this.fetchData(id)
        },
        methods: {
            fetchData(id) {
                this.$adminApi.task_detail({id:id}).then(response => {
                    this.postForm = response.data
                }).catch(err => {
                    console.log(err)
                })
            },
            submitForm() {
                this.loading = true
                this.$adminApi.task_start(this.postForm).then(response => {
                    this.$adminApi.attach_download({id:this.postForm.attach_id}).then(response => {
                        window.open(response.data, '_blank') // 新窗口打开外链接
                    });

                    this.$notify({
                        title: '成功',
                        message: '开始任务',
                        type: 'success',
                        duration: 2000
                    })
                    if (!response.code) {
                        this.$router.push({
                            name: 'myTask',
                            params: {  }
                        })
                    }
                    this.loading = false
                }).catch((e)=>{
                    this.loading = false
                })
            },

        }
    }
</script>
